package gateway

import (
	"context"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"net/http"

	"github.com/go-kit/kit/endpoint"
)

type Info struct {
	Name      string
	BuildId   int
	WsAddress string
}
type InfoService struct {
}

func GinInfo(c *gin.Context) {
	c.JSON(http.StatusOK, Info{
		Name:      "test",
		BuildId:   1,
		WsAddress: "localhost",
	})
}

func (i InfoService) Info() Info {
	return Info{
		Name:      "test",
		BuildId:   1,
		WsAddress: "localhost",
	}
}

type InfoRequest struct {
}
type InfoResponse struct {
	I Info
}

func MakeInfoEndpoint(i InfoService) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		_ = request.(InfoRequest)
		I := i.Info()
		return InfoResponse{I: I}, nil
	}
}

type Endpoints struct {
	Info endpoint.Endpoint
}

func NewHTTPHandler(endpoints Endpoints) http.Handler {
	m := gin.Default()
	m.Handle("GET", "/info", GinInfo)
	return m
}
func DecodeInfoRequest(_ context.Context, r *http.Request) (interface{}, error) {
	var req InfoRequest
	//err := json.NewDecoder(r.Body).Decode(&req)
	return req, nil
}
func EncodeInfoResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	return json.NewEncoder(w).Encode(response)
}
